package gsw.tool.common;

import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.support.annotation.DrawableRes;
import android.util.Log;

import java.util.ArrayList;

import gsw.tool.R;

/**
 * 分享工具类
 */
public class ToolShare {
    private static String TAG = "ToolShare";
    public static String WEIXIN = "com.tencent.mm";//微信的包名
    public static String QQ = "com.tencent.mobileqq";//QQ的包名

    /**
     * 分享文本
     */
    public static void shareText(Context context, String title) {
        Intent textIntent = new Intent(Intent.ACTION_SEND);
        textIntent.setType("text/plain");
        textIntent.putExtra(Intent.EXTRA_TEXT, title);
        context.startActivity(Intent.createChooser(textIntent, context.getString(R.string.share)));
    }

    /**
     * 分享文本:指定包
     */
    public static void shareText(Context context, String title, String packageName) {
        Intent textIntent = new Intent(Intent.ACTION_SEND);
        textIntent.setPackage(packageName);
        textIntent.setType("text/plain");
        textIntent.putExtra(Intent.EXTRA_TEXT, title);
        context.startActivity(Intent.createChooser(textIntent, context.getString(R.string.share)));
    }

    /**
     * 分享单张资源图片
     */
    public static void shareResourceImage(Context context, int resID) {
        String path = getResourcesUri(context, resID);
        Intent imageIntent = new Intent(Intent.ACTION_SEND);
        imageIntent.setType("image/jpeg");
        imageIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(path));
        context.startActivity(Intent.createChooser(imageIntent, context.getString(R.string.share)));
    }

    /**
     * 分享单张资源图片
     */
    public static void shareResourceImages(Context context, int[] resIDs) {
        if (Tool.isEmpty(resIDs)) {
            return;
        }
        ArrayList<Uri> uris = new ArrayList<>();
        for (int resID : resIDs) {
            uris.add(Uri.parse(getResourcesUri(context, resID)));
        }
        Intent imageIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
        imageIntent.setType("image/jpeg");
        imageIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
        context.startActivity(Intent.createChooser(imageIntent, context.getString(R.string.share)));
    }

    /**
     * 获取资源的路径
     *
     * @param id
     * @return
     */
    private static String getResourcesUri(Context context, @DrawableRes int id) {
        Resources resources = context.getResources();
        String uriPath = ContentResolver.SCHEME_ANDROID_RESOURCE + "://" +
                resources.getResourcePackageName(id) + "/" +
                resources.getResourceTypeName(id) + "/" +
                resources.getResourceEntryName(id);
        Log.d(TAG, "Uri:" + uriPath);
        return uriPath;
    }
}
